<template>
	<view>
		<cu-custom :bgColor="'bg-' + theme.backgroundColor" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">团长数据</block>
		</cu-custom>
		<view>
			<zbTable
				:show-header="true"
				:columns="column"
				:stripe="true"
				:fit="false"
				:border="true"
				@row-click="rowClick"
				@search="changeSearch"
				:page="page"
				:data="data"
				@export="handExport"
				@changePage="changePage"></zbTable>
		</view>
	</view>
</template>

<script>
const app = getApp();
import util from "utils/util";
import api from "utils/api";
import config from "@/config/env.js";
import zbTable from "components/zb-table/zb-table.vue";
export default {
	components: {
		zbTable,
	},
	data() {
		return {
			theme: app.globalData.theme, //全局颜色变量
			page: {
				current: 1,
				size: 10,
				total: 0,
			},
			column: [
				{
					label: "#",
					type: "index",
					width: 40,
				},
				{
					label: "团长昵称",
					name: "tzNickName",
					overHidden: true,
				},
				{
					label: "团长手机号",
					name: "tzPhone",
					search: true,
				},
				{
					label: "邀请总人数",
					name: "tzInviteNum",
				},
				{
					label: "发红包总金额",
					name: "tzSendRedPackMoney",
				},
				{
					label: "用户领取红包总金额",
					name: "tzReceiveRedPackMoney",
				},
				{
					label: "发积分总数量",
					name: "tzSendIntegral",
					overHidden: true,
				},
				{
					label: "用户领取积分数量",
					name: "tzReceiveIntegral",
				},
				{
					label: "设置奖品数量",
					name: "tzSetPrizeNum",
				},
				{
					label: "用户领取奖品数量",
					name: "tzReceivePrizeNum",
				},
				{
					label: "发放优惠券数量",
					name: "tzSetCouponNum",
				},
				{
					label: "用户领取优惠券数量",
					name: "tzReceiveCouponNum",
				},
			],
			data: [],
			searchform: {},
		};
	},
	onLoad() {
		this.getData();
	},
	onShow() {},
	//方法
	methods: {
		rowClick() {},
		changeSearch(form) {
			this.searchform = form;
			this.page.current = 1;
			this.getData();
		},
		changePage(e) {
			this.page.current = e.current;
			this.getData();
		},
		handExport() {
			uni.showLoading({
				title: "加载中",
				mask: true,
			});
			let that = this;
			let search = "?";
			for (let key in this.searchform) {
				search += key + "=" + this.searchform[key] + "&";
			}
			//删除最后一个&
			search = search.substring(0, search.length - 1);
			uni.downloadFile({
				url: config.basePath + "/liveapi/excel/export/liveAndTzData" + search,
				header: {
					"client-type": "MA", //客户端类型小程序
					"app-id": uni.getAccountInfoSync().miniProgram.appId, //小程序appId
					"third-session": uni.getStorageSync("third_session")
						? uni.getStorageSync("third_session")
						: "",
				},
				success: (file) => {
					let fileManagerObj = uni.getFileSystemManager();
					let FileName = decodeURIComponent(file.header.FileName);
					const filePath = `${wx.env.USER_DATA_PATH}/${FileName}`;
					console.log(fileManagerObj, "1111111");
					fileManagerObj.saveFile({
						tempFilePath: file.tempFilePath,
						filePath: filePath,
						success: (res) => {
							console.log(res, "resssssssss");
							uni.hideLoading();
							uni.showModal({
								title: "提示",
								content:
									"ios:点击预览页面右上角三个点-->用其他应用打开-->存储到文件\n" +
									"安卓:点击预览页面右上角三个点-->保存到手机",
								showCancel: false,
								success: ({ confirm, cancel }) => {
									if (confirm) {
										var filePath = res.savedFilePath;
										uni.openDocument({
											//新开页面打开文档，支持格式：doc, xls, ppt, pdf, docx, xlsx, pptx。
											filePath: filePath,
											showMenu: true,
											success: function (res) {
												console.log("打开文档成功");
											},
										});
									}
								},
							});
						},
						fail: (err) => {
							uni.hideLoading();
							uni.showToast({
								title: "导出失败1",
								icon: "none",
							});
						},
					});
				},
				fail: (error) => {
					uni.hideLoading();
					uni.showToast({
						title: "导出失败2",
						icon: "none",
					});
					uni.hideLoading();
				},
			});
		},
		getData(form) {
			let para = Object.assign(this.page, this.searchform);
			api.liveAndTzDataPage(util.filterForm(para)).then((res) => {
				this.data = res.data.records;
				this.page.total = res.data.total;
				// if (pointsRecord.length < this.page.size) {
				// 	this.loadmore = false;
				// }
			});
		},
	},
};
</script>

<style lang="scss" scoped></style>
